<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>

<div class="panel panel-default">
        <div class="panel-heading">
            <h3 class="panel-title">订单信息</h3>
        </div>
        <style>
            .panel-heading ul, .panel-heading ul li {
                float: left;
                list-style: none;
            }

            li .Wdate {
                height: 30px;
                width: 90px;
            }

            .panel-heading li {
                margin: 0 10px;
            }

            .panel-heading li, .panel-heading li select {
                height: 30px;
                line-height: 30px;
            }
        </style>
        <div class="panel-heading">
            <form action="/admin/queryOrderList.jspa?" method="post">

            <ul>
                    <li>订单状态</li>
                    <li><select name="orderStatus" id="orderState">
                        <c:if test="${status.equals('1')}">
                        <option value="1" selected>所有订单</option>
                        </c:if>
                        <c:if test="${!status.equals('1')}">
                            <option value="1">所有订单</option>
                        </c:if>
                        <c:forEach items="${orderService.findOrderState()}" var="ord">
                            <c:if test="${status.equals(ord)}">
                                <option value="${ord}" selected>${ord}</option>
                            </c:if>
                            <c:if test="${!status.equals(ord)}">
                                <option value="${ord}">${ord}</option>
                            </c:if>
                        </c:forEach>
                    </select></li>
                    <c:choose>
                        <c:when test="${!empty begins && !empty ends}">
                            <li>开始日期</li>
                            <li><input name="begin" value="${begins}" class="Wdate" type="text"
                                       onfocus="WdatePicker({dateFmt:'yyyy-MM-dd'})"/></li>
                            <li>结束日期</li>
                            <li><input name="end" class="Wdate" type="text" value="${ends}"
                                       onfocus="WdatePicker({dateFmt:'yyyy-MM-dd'})"/></li>
                        </c:when>
                        <c:when test="${!empty dates}">
                            <li>开始日期</li>
                            <li><input name="begin" value="${dates}" class="Wdate" type="text"
                                       onfocus="WdatePicker({dateFmt:'yyyy-MM-dd'})"/></li>
                            <li>结束日期</li>
                            <li><input name="end" class="Wdate" type="text" value="${dates}"
                                       onfocus="WdatePicker({dateFmt:'yyyy-MM-dd'})"/></li>
                        </c:when>
                        <c:otherwise>
                            <li>开始日期</li>
                            <li><input name="begin" value="${begins}" class="Wdate" type="text"
                                       onfocus="WdatePicker({dateFmt:'yyyy-MM-dd'})"/></li>
                            <li>结束日期</li>
                            <li><input name="end" class="Wdate" type="text" value="${ends}"
                                       onfocus="WdatePicker({dateFmt:'yyyy-MM-dd'})"/></li>
                        </c:otherwise>
                    </c:choose>
                    <li><input class="form-control" style="width: 60px" type="submit" value="查 询"/></li>
                    <li><input type="button" value="导出订单" id="group-export" onclick="exportOrder()"/></li>
                    
                </ul>
                </form>
        </div>
</div>
    <div class="panel-body">
        <input class="form-control" style="width: 80px" type="submit" value="批量完成" id="butt" onclick="goCon()"/>
        <form name="form1" id="fors" method = "post">

                <table class="table">
                <thead>
                <tr style="line-height:47px">
                    <th><label class="check"><input type="checkbox" id="dd" ></label></th>
                    <th>订单编号</th>
                    <th>下单菜品</th>
                    <th>订单状态</th>
                    <th>联系方式</th>
                    <th>下单时间</th>
                    <th>付款金额</th>
                    <th>交易方式</th>
                    <th>备注</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                <c:forEach items="${orders}" var="root">
                    <tr class="item-comment comment-quote info" id="${root.orderNum}" name="${root.orderState}">
                        <td><label class="check"><input type="checkbox" name="nums" value="${root.orderNum}"></label></td>
                        <td>${root.orderNum}</td>
                        <td>
                            <c:forEach items="${root.orderItems}" var="item">
                                ${item.itemName}&nbsp;&nbsp;&nbsp;&nbsp;×${item.amount}<br>
                            </c:forEach>
                        </td>
                        <td>${root.orderState}</td>
                        <td>${root.cellPhone}&nbsp;${root.userName}</td>
                        <td>${root.orderDate}</td>
                        <td>${root.totalPrice}</td>
                        <td>${root.payMethod}</td>
                        <td>${root.memo}</td>
                        <td>
                            <c:choose>
                                <c:when test="${root.orderState.equals('已确认')}">
                                    <input type="button" value="完成"
                                           onclick="goConfims(this)" />
                                    <input type="button" value="餐厅取消" onClick="goCancel(this)" id="${root.orderNum}"/>
                                </c:when>
                                <c:when test="${root.orderState.equals('待退款')}">
                                    <input type="button" value="退款确认" id="${root.orderNum}" onclick="refund(this)" />
                                </c:when>
                                <c:when test="${root.orderState.equals('已取消')}">
                                    <input type="hidden" value="已取消" disabled ='true'/>
                                </c:when>
                                <c:otherwise>
                                </c:otherwise>
                            </c:choose>
                        </td>
                    </tr>
                </c:forEach>

                </tbody>
            </table>
        </form>

    </div>
</div>
<!-- END DEFAULT DATATABLE -->

<!-- MESSAGE BOX-->
<div id="ifm"></div>
<script>

$("#dd").change(function(){
    var flag= $("input[type=checkbox]:eq(0)").prop("checked");
    $('input[type="checkbox"]:gt(0)').prop("checked",flag);
});
$("input[type='checkbox']:gt(0)").click(function(){
    var leng = $("input[type='checkbox']:gt(0)").length;
    var len = $("input[type='checkbox']:gt(0):checked").length;
    $("#dd").prop("checked",leng==len);
});


    var isDo = 0;
    function exportOrder() {
        if (isDo == 1){
            alert("hello");
            return;
        }
        var dd = $("input[name=begin]").val();
        var st = $("input[name=end]").val();
        var state = $("#orderState").val();
        var b = dd,e = st;
        if (b != '' && e != '') {
            isDo = 1;
            $("#group-export").html("正在导出 ... ");
            $('#ifm').html('<iframe id="downFrame" name="downFrame" width="0" height="0" src="'+
                    '/admin/order/orderExport.jspx?begin=' + b + '&end=' + e + '&state='+state+'"></iframe>');
            isDo=0;
//            $("#downFrame").attr("src", "/admin/statistics/downOrder.jspx?type=fin&arrive=" + a + "&t=" + (new Date()));
        }
    }
    function downFrameOver() {
        alert("hello");
        isDo = 0;
        $("#group-export").html("导出订单");
    }
    //查询订单
    function func() {
        var dd = $("input[name=begin]").val();
        var st = $("input[name=end]").val();
        var sta = $("#orderState").val();
        window.location.href = "/admin/queryOrderList.jspa?begin="+dd+"&end"+st+"&orderStatus="+sta;
    }
//    餐厅取消订单
    function goCancel(ths) {
        var dd = $("input[name=begin]").val();
        var st = $("input[name=end]").val();
        var sta = $("#orderState").val();
        var ids = $(ths).parent().parent().prop("id");
        layer.open({
            content: '你确定取消订单吗？', btn: ['确认', '取消'], yes: function (index) {
                json("/admin/cancelOrder2.jspa", {orderNum: ids}, function (data) {
                    if (data.code == 0) {
                        $(ths).prop("type","hidden");
                        $(ths).prev().prop("type","hidden");
                        var dd = $(ths).parent().prev().prev().prev().prev().prev().prev();
                        dd.text("餐厅取消");
                    } else {
                        layer.open({content: data.description, time: 3});
                    }
                });
            },time:2
        });
    }
//    确认订单
    function goConfims(ths) {
        var dd = $("input[name=begin]").val();
        var st = $("input[name=end]").val();
        var sta = $("#orderState").val();
        var ims = $("input[type='checkbox']:gt(0):checked").val();
        var ids = $(ths).parent().parent().prop("id");
        $.ajax({
            type: 'post', url: '/admin/updateOrderStatus.jspa', dataType: 'text', data: 'orderNum=' + ids,
            success: function (o) {
                $(ths).prop("type","hidden");
                $(ths).next().prop("type","hidden");
                var dd = $(ths).parent().prev().prev().prev().prev().prev().prev();
                dd.text("已完成");
            }
        });
    };
//批量确认订单
function goCon() {
    var dd = $("input[name=begin]").val();
    var st = $("input[name=end]").val();
    var sta = $("#orderState").val();
    var nums = $("input[type='checkbox']:gt(0):checked").serialize();
    $.ajax({
        type: 'post', url: '/admin/statusChange.jspa', dataType: 'text', data: nums,
        success: function (data) {
            var i = $.parseJSON(data);
            if(i.code==0) {
                window.location.href = "/admin/queryOrd.jspa";
            }else{
                layer.open({content:"订单操作有误",time:1});
            }
        }
    });
}
    //退款确认处理
    function refund(fud){
        var fd = $(fud).prop("id");
        if($(fud).prop("value")=="已取消") return;
        layer.open({
            content: '你确定取消订单吗？', btn: ['确认', '取消'], yes: function (index) {
                $.ajax({
                    type: 'post', url: '/admin/refund.jspa', dataType: 'text', data: "orderNum="+fd,
                    success: function (data) {
                        var i = $.parseJSON(data);
                        if(i.code==0) {
                            layer.open({content:"退款成功",time:1});
                            $(fud).prop("type","hidden");
                            $(fud).prop("value","已取消");
                            var dd = $(fud).parent().prev().prev().prev().prev().prev().prev();
                            dd.text("已取消");

//                    window.location.href = "/admin/queryOrd.jspa";
                        }else{
                            layer.open({content:"退款处理操作不当",time:3});
                        }
                    }
                });
            }
        });
    }
</script>
<%--<script src="/admin/statics/My97DatePicker/WdatePicker.js"></script>--%>
<script src="/statics/admin/js/My97DatePicker/WdatePicker.js"></script>